#include "gtest/gtest.h"
#include "../BaseUtil/MyTest.h"
#include "PANEImp.h"
namespace PANEImp {

class PANEButtonElementTest : public testing::Test {
protected:
    virtual void SetUp() {
    	doc = new PANEDocument();
    	btn = doc->createPANEButtonElement();

    	btn->setNormalImage( "data/btn/play0.png" );
    	btn->setMouseDownImage( "data/btn/play1.png" );
    	btn->setMouseOverImage( "data/btn/play2.png" );
    }
    virtual void TearDown() {
    	delete doc;
    }

    static bool isSameImage( IplImage * img1, IplImage * img2 ) {
    	if( img1->height != img2->height ) return false;
    	if( img1->width != img2->width ) return false;

    	for( int i = 0; i < img1->height; ++i )
    		for( int j = 0; j < img1->width; ++j ) {
    			const CvScalar dot1 = cvGet2D( img1, i, j );
    			const CvScalar dot2 = cvGet2D( img2, i, j );
    			if( dot1.val[0] != dot2.val[0] ) return false;
    			if( dot1.val[1] != dot2.val[1] ) return false;
    			if( dot1.val[2] != dot2.val[2] ) return false;
    		}
    	return true;
    }

    PANEDocument * doc;
    PANEButtonElement * btn;
};

TEST_F(PANEButtonElementTest, LoadUpImageFiles) {
	ASSERT_EQ(
			VisualButtonForPANE::NORMAL_IMAGE |
			VisualButtonForPANE::MOUSE_OVER_IMAGE |
			VisualButtonForPANE::MOUSE_DOWN_IMAGE,
			btn->getLoadedImages()
		);
}

TEST_F(PANEButtonElementTest, Render) {
	const unsigned long width = btn->getWidth();
	const unsigned long height = btn->getHeight();
	ASSERT_EQ(54ul, width);
	ASSERT_EQ(54ul, height);

    IplImage * output = cvCreateImage( cvSize(width, height), IPL_DEPTH_32S, 3 );
    ASSERT_FALSE( *(output->imageData) );
	btn->render( output );
    ASSERT_TRUE( *(output->imageData) );
	cvReleaseImage( &output );
}

TEST_F(PANEButtonElementTest, NormalButton) {
	IplImage * output = cvCreateImage( cvSize(btn->getWidth(), btn->getHeight()), IPL_DEPTH_32S, 3 );
	btn->render( output );
    ASSERT_TRUE( isSameImage( output, btn->cvImgs_[VisualButtonForPANE::ButtonNormalState]) );
	cvReleaseImage( &output );
}

TEST_F(PANEButtonElementTest, MouseOverButton) {
	Event * evt = btn->createEvent( MouseEventImp::EVENT_MOUSE_OVER );
	ASSERT_TRUE( evt );
	btn->dispatchEvent( evt );

	IplImage * output = cvCreateImage( cvSize(btn->getWidth(), btn->getHeight()), IPL_DEPTH_32S, 3 );
	btn->render( output );
    ASSERT_TRUE( isSameImage( output, btn->cvImgs_[VisualButtonForPANE::ButtonMouseOverState]) );
	cvReleaseImage( &output );
}
TEST_F(PANEButtonElementTest, MouseDownButton) {
	Event * evt = btn->createEvent( MouseEventImp::EVENT_MOUSE_DOWN );
	ASSERT_TRUE( evt );
	btn->dispatchEvent( evt );

	IplImage * output = cvCreateImage( cvSize(btn->getWidth(), btn->getHeight()), IPL_DEPTH_32S, 3 );
	btn->render( output );
    ASSERT_TRUE( isSameImage( output, btn->cvImgs_[VisualButtonForPANE::ButtonMouseDownState]) );
	cvReleaseImage( &output );
}
TEST_F(PANEButtonElementTest, MouseOutButton) {
	Event * evt = btn->createEvent( MouseEventImp::EVENT_MOUSE_OUT );
	ASSERT_TRUE( evt );
	btn->dispatchEvent( evt );

	IplImage * output = cvCreateImage( cvSize(btn->getWidth(), btn->getHeight()), IPL_DEPTH_32S, 3 );
	btn->render( output );
    ASSERT_TRUE( isSameImage( output, btn->cvImgs_[VisualButtonForPANE::ButtonNormalState]) );
	cvReleaseImage( &output );
}

}
